/**
 *
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 *
 *
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 *
 * */
package com.ruipengkj.view.memberView;

import java.awt.CardLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Date;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;

import com.dianlian.pss.inter.OperateException;
import com.dianlian.pss.inter.ws.UserMgr;
import com.ruipengkj.ApplicationContext;
import com.ruipengkj.CardComponentListener;
import com.ruipengkj.bean.TSendCardRecord;
import com.ruipengkj.bean.TUser;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.JPanelBoxLayoutComponent;
import com.ruipengkj.component.JPanelFlowLayoutComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.component.RPJButtonWhite;
import com.ruipengkj.service.TUserService;
import com.ruipengkj.view.base.BasePanel;
/**
 * 说明：
 * 补卡UI
 * @Author：黄盛强
 * @Date：2012-03-20
 */
public class MemberLostCardPanel extends BasePanel {


	private static final long serialVersionUID = 1L;

//	private JLabelJTextFieldComponent usernameTF = null; //用户名
	private JLabelJTextFieldComponent mobileTF = null;  //手机号码
	private JLabelJTextFieldComponent mobileVerifiTF=null;//输入卡验证码
//	private JLabelJPasswordFieldComponent oldPassTF = null; //旧密码

	private JLabelJTextFieldComponent CardNumTF = null; //新卡号
	private JLabelJTextFieldComponent number = null;//会员编号

	private JButton sureButton ,cancelButton; //确定清空
	private JButton lostButton1,returnButton; //补卡返回

	private JButton get_mobileVerifiButton,ok_mobileVerifiButton;//获取手机验证码按钮

	private TUserService tUserService;

	private JPanel cardPanel;
	private CardLayout cardLayout;
	private TUser tUser;
	private Timer timer=new Timer(1000, this);
	private int timeNumber=0;
	public MemberLostCardPanel() {
		tUserService = BuildSpringApplication.getInstance().getTUserServiceImpl();
		this.setLayout(new FlowLayout());
		this.init();
		this.initListener();
		ApplicationContext.listenCard(CardNumTF.getText(), ApplicationContext.CardType.CT_DLInited);
		CardNumTF.getText().addComponentListener(new CardComponentListener());
	}


	private void init() {
//		usernameTF = new JLabelJTextFieldComponent("*", "用  户  名：", 30);
//		JPanel mobile_jp=uiFactory.createFlowJPanel(FlowLayout.LEFT);
		mobileTF = new JLabelJTextFieldComponent("*", "手  机  号：", 30);
		get_mobileVerifiButton=new RPJButtonWhite("获取验证码(0)");
		mobileTF.add(get_mobileVerifiButton);
//		mobile_jp.add(mobileTF);
//		mobile_jp.add(get_mobileVerifiButton);
//		JPanel mobile_ok_jp=uiFactory.createFlowJPanel(FlowLayout.LEFT);
		mobileVerifiTF=new JLabelJTextFieldComponent("*", "手机验证码：", 30);
//		ok_mobileVerifiButton=new RPJButtonWhite("确定");
		mobileVerifiTF.getText().setEnabled(false);
//		ok_mobileVerifiButton.setEnabled(false);
//		mobileVerifiTF.add(ok_mobileVerifiButton);
//		mobile_ok_jp.add(mobileVerifiTF);
//		mobile_ok_jp.add(ok_mobileVerifiButton);

//		oldPassTF = new JLabelJPasswordFieldComponent("*", "卡密码确认：", 30);
		sureButton = new RPJButtonBlue("确定(Ctrl+Y)");
		cancelButton = new RPJButtonGray("清空");
		JPanel btns1 = new JPanelFlowLayoutComponent(new Component[]{
				sureButton, cancelButton
		}, FlowLayout.LEFT);
		JPanel card1 = new JPanelBoxLayoutComponent(new Component[]{
				 mobileTF, mobileVerifiTF,btns1
		}, BoxLayout.Y_AXIS);
//		JPanel card1 = new JPanelBoxLayoutComponent(new Component[]{
//				 mobileTF, mobileVerifiTF,oldPassTF,btns1
//		}, BoxLayout.Y_AXIS);

		//number = new JLabelJTextFieldComponent("   *", "会员编号：",30);
		//number.getText().setEditable(false);
		JPanel cardNum_jp=uiFactory.createFlowJPanel(FlowLayout.LEFT);
		CardNumTF = new JLabelJTextFieldComponent("  *", "补卡卡号：",30);
		cardNum_jp.add(CardNumTF);
		lostButton1= new RPJButtonBlue("确定(CTRL+Y)");
		returnButton =new RPJButtonGray("返回");
		JPanel btns2 = new JPanelFlowLayoutComponent(new Component[]{
				lostButton1, returnButton
		}, FlowLayout.LEFT);
		JPanel card2 = new JPanelBoxLayoutComponent(new Component[]{
				cardNum_jp, btns2
		}, BoxLayout.Y_AXIS);
//		JPanel card2 = new JPanelBoxLayoutComponent(new Component[]{
//				number, cardNum_jp, btns2
//		}, BoxLayout.Y_AXIS);

		cardLayout = new CardLayout();
		cardPanel =uiFactory.createCardJPanel(cardLayout);
		cardPanel.add(card1, "1");
		cardPanel.add(card2, "2");
		cardPanel.setBorder(BorderFactory.createTitledBorder("卡挂失"));
		this.add(cardPanel);
	}

	private void initListener(){
		get_mobileVerifiButton.addActionListener(this);
		sureButton.addActionListener(this);
		sureButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		cancelButton.addActionListener(this);
		lostButton1.addActionListener(this);
		lostButton1.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		returnButton.addActionListener(this);
	}

	//卡挂失
	private void lost() {
//		String nameT=usernameTF.getText().getText().trim(); //用户名
		String mobileT=mobileTF.getText().getText().trim();  //电话号码
		//String oldPassT=oldPassTF.getText().getText().trim(); //旧密码
//		if(nameT.equals("")||nameT.length()>15){
//			JOptionPane.showMessageDialog(null, "用户名不能为空，且不能超过20个字");
//			usernameTF.getText().requestFocus();
//			return ;
//		}
		if(mobileT.equals("")||!MatcherFormatUtil.checkMobileScheme(mobileT)){
			JOptionPane.showMessageDialog(null, "手机号码不能为空，请输入正确的手机号码");
			mobileTF.getText().requestFocus();
			return ;
		}

//		if(oldPassT.equals("")||oldPassT.length()<6||oldPassT.length()>15){
//			JOptionPane.showMessageDialog(null, "旧密码不能为空，不少于6位且不能大于15位");
//			oldPassTF.getText().requestFocus();
//			return ;
//		}
		tUser=new TUser();
//		tUser.setUserName(nameT);
		tUser.setMobile(mobileT);
		//tUser.setPwd(oldPassT);
		try {
			List<TUser> tUserList = tUserService.findService(tUser);
			if (tUserList != null && tUserList.size() != 0) {
				tUser = tUserList.get(0);
				JOptionPane.showMessageDialog(null, "资料填写正确，请设置新卡号！");
				cardLayout.show(cardPanel, "2");
				//number.getText().setText(tUser.getUserID());
//				CardNumTF.getText().setText(UUID.GenUUID15());
				added();
			}else{
				JOptionPane.showMessageDialog(null, "不存在该会员信息，请重新输入！");
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	//补卡
	private void added() {
		//String CardNum=DateUtil.formatD(new Date(), "MMddHHmmss"); //新卡号
//		String CardNum=CardNumTF.getText().getText().trim(); //新卡号
//		if(CardNum.equals("")||CardNum.length()>15){
//			JOptionPane.showMessageDialog(null, "新卡号不能为空，且不能超过15个字");
//			CardNumTF.getText().requestFocus();
//			return ;
//		}
//		try {
//			if(tUserService.getByCardNumService(CardNumTF.getText().getText().trim())!=null){
//				JOptionPane.showMessageDialog(null, "该卡号已经存在，请重新输入");
//				CardNumTF.getText().requestFocus();
//				return;
//			}
//			Date date=new Date();
//			TSendCardRecord tSendCardRecord=new TSendCardRecord();
//			tSendCardRecord.setSendCardRecordID(UUID.GenUUID15());
//			tSendCardRecord.setCardNum(CardNum);
//			tSendCardRecord.setSendCardTime(date);
//			tUser.setSendCardTime(date);
//			tUser.setCardNum(CardNum);
//			tUser.getTSendCardRecords().add(tSendCardRecord);
//			tUserService.saveOrUpdateService(tUser);
//			BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("会员补卡，新卡号为：+"+CardNumTF.getText().getText().trim());
//			JOptionPane.showMessageDialog(null, "操作成功，此次卡号为：" + CardNum);
//			cardLayout.show(cardPanel, "1");
//			this.clear();
//		} catch (Exception e1) {
//			e1.printStackTrace();
//			JOptionPane.showMessageDialog(null, "操作失败，请重新操作");
//		}

		String CardNum=CardNumTF.getText().getText().trim(); //新卡号
		String mobileT=mobileTF .getText().getText().trim();  //电话号码

		if(mobileT.equals("")||!MatcherFormatUtil.checkMobileScheme(mobileT)){
			JOptionPane.showMessageDialog(null, "手机号码不能为空，请输入正确的手机号码");
			mobileTF.getText().requestFocus();
			return ;
		}
		if(CardNum.equals("")||CardNum.length()>15){
			JOptionPane.showMessageDialog(null, "新卡号不能为空，且不能超过15个字");
			CardNumTF.getText().requestFocus();
			return ;
		}
		tUser=new TUser();
		tUser.setMobile(mobileT);
		try {
			if(tUserService.getByCardNumService(CardNumTF.getText().getText().trim())!=null){
				JOptionPane.showMessageDialog(null, "该卡号已经存在，请重新输入");
				CardNumTF.getText().requestFocus();
				return;
			}
			List<TUser> tUserList = tUserService.findService(tUser);
			if (tUserList != null && tUserList.size() != 0) {
			tUser = tUserList.get(0);
			}
			Date date=new Date();
			TSendCardRecord tSendCardRecord=new TSendCardRecord();
			tSendCardRecord.setSendCardRecordID(UUID.GenUUID15());
			tSendCardRecord.setCardNum(CardNum);
			tSendCardRecord.setSendCardTime(date);
			tUser.setSendCardTime(date);
			tUser.setCardNum(CardNum);
			tUser.getTSendCardRecords().add(tSendCardRecord);
			 boolean r;
			 UserMgr um = ApplicationContext.conn.getSiteUserMgr();
//			 InputStreamReader stdin   =   new   InputStreamReader(System.in);//
//		     BufferedReader   bufin   =   new BufferedReader(stdin);
		     String checkCode = mobileVerifiTF.getText().getText().trim();
		     r = um.resendCard(tUser.getMobile(), tUser.getCardNum(), checkCode);

			//写卡
			if (!ApplicationContext.CardUtilThread.sendCard(tUser.getUserID(), tUser.getMobile(), tUser.getRealName())){
				Util.showMessageDialog("写卡失败，请重新放入卡片！");
				return;
			}


			tUserService.saveOrUpdateService(tUser);
			BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("会员补卡，新卡号为：+"+CardNumTF.getText().getText().trim());
			JOptionPane.showMessageDialog(null, "操作成功，此次卡号为：" + CardNum);
//			cardLayout.show(cardPanel, "1");
			this.clear();
		} catch (Exception e1) {
			e1.printStackTrace();
			JOptionPane.showMessageDialog(null, "操作失败，请重新操作");
		}
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		if(timeNumber==0){
			timer.stop();
			get_mobileVerifiButton.setEnabled(true);
			//mobileVerifiTF.getText().setEnabled(false);
			//mobileVerifiTF.getText().setText("");
//			ok_mobileVerifiButton.setEnabled(false);
			get_mobileVerifiButton.requestFocus();
		}else{
			timeNumber--;
			get_mobileVerifiButton.setText("获取验证码("+timeNumber+")");
		}

		if(e.getSource()==sureButton){
			//卡挂失
			this.lost();
		}else if(e.getSource()==lostButton1){
			//补卡
			this.added();
		}else if(e.getSource()==cancelButton){
			//清空
			this.clear();
		}else if(e.getSource()==returnButton){
			//返回
			cardLayout.show(cardPanel, "1");
		}else if(e.getSource()==get_mobileVerifiButton){
			//获取手机验证码
//			String mobileT=mobileTF .getText().getText().trim();  //电话号码
//			if(mobileT.equals("")||!MatcherFormatUtil.checkMobileScheme(mobileT)){
//				JOptionPane.showMessageDialog(null, "手机号码不能为空，请输入正确的手机号码");
//				mobileTF.getText().requestFocus();
//				return ;
//			}
//			timer.start();
//			timeNumber=60;
//			mobileVerifiTF.getText().requestFocus();
//			get_mobileVerifiButton.setEnabled(false);
//			mobileVerifiTF.getText().setEnabled(true);
//			ok_mobileVerifiButton.setEnabled(true);
			//获取手机验证码
			String mobileT=mobileTF .getText().getText().trim();  //电话号码
			if(mobileT.equals("")||!MatcherFormatUtil.checkMobileScheme(mobileT)){
				JOptionPane.showMessageDialog(null, "手机号码不能为空，请输入正确的手机号码");
				mobileTF.getText().requestFocus();
				return ;
			}
			 UserMgr um = ApplicationContext.conn.getSiteUserMgr();
			 boolean r;
		     try {
				r = um.sendCheckCode(mobileT, UserMgr.CHECK_CODE_FOR_RESECOND_CARD);
			} catch (OperateException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			timer.start();
			timeNumber=180;
			mobileVerifiTF.getText().requestFocus();
			get_mobileVerifiButton.setEnabled(false);
			mobileVerifiTF.getText().setEnabled(true);
			//ok_mobileVerifiButton.setEnabled(true);
		}
	}
	@Override
	public void clear() {
		//清空
//		usernameTF.getText().setText(""); //会员名
		mobileTF .getText().setText("");  //电话号码
//		oldPassTF.getText().setText(""); //旧密码
		CardNumTF.getText().setText(""); //新卡号
		mobileTF.getText().requestFocus();
//		usernameTF.getText().requestFocus();

		ApplicationContext.listenCard(CardNumTF.getText(), ApplicationContext.CardType.CT_DLInited);
	}
}